package datamining.miner.general;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;

import settings.Settings;

import datamining.sequence.HPSequence;
import datamining.sequence.ItemSet;
import datamining.sequence.Sequence;

/**
 * 创建于 2012-11-18 下午8:27:57
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn)
 * 
 *         Copyright 2012 xujinghu
 * 
 *         Lisence: Apache
 */
public class SequenceDataBase<ItemType> {

	final SortedSet<ItemType> allItems;
	final SortedSet<ItemType> freqItems;
	final Map<ItemType, Frequency> ifreq;

	public SequenceDataBase(final List<Sequence<ItemType>> sequences,
			final Settings settings) {
		ifreq = new HashMap<ItemType, Frequency>();

		//计算序列中项的频繁度
		for (Iterator<Sequence<ItemType>> it = sequences.iterator(); it.hasNext();) {
			Sequence<ItemType> o_sequence = it.next();
			final Frequency sfreq = settings.getFrequency(o_sequence);
			for(ItemSet<ItemType> itemSet : o_sequence.getItemSets())
				for(ItemType item : itemSet.getItems()){
					final Frequency freq = ifreq.get(item);
					if (freq == null) {
						ifreq.put(item, sfreq.clone());
					} else {
						freq.add(sfreq);
					}
				}
		}

		freqItems = initialItems(settings, ifreq);
		allItems = initialItems(settings, ifreq);
		
		//移除非频繁项
		for (final Entry<ItemType, Frequency> entry : ifreq.entrySet()) {
			allItems.add(entry.getKey());
			if (settings.minFreq.compareTo(entry.getValue()) <= 0) {
				freqItems.add(entry.getKey());
			}
		}
	}

	public SortedSet<ItemType> frequentItems() {
		return freqItems;
	}

	private SortedSet<ItemType> initialItems(final Settings settings,
			final Map<ItemType, Frequency> m_item2freq) {
		SortedSet<ItemType> sorted_items;
		if (settings.naturalOrderedItemLabels) {
			sorted_items = settings.reverseOrderedItemLabels ? new TreeSet<ItemType>(
					new Comparator<ItemType>() {
						@SuppressWarnings("unchecked")
						public int compare(
								final ItemType o1,
								final ItemType o2) {
							return ((Comparable) o2)
									.compareTo(o1);
						}
					})
					: new TreeSet<ItemType>();
		} else {

			// sort items according to their frequency
			sorted_items = settings.reverseOrderedItemLabels ? new TreeSet<ItemType>(
					new Comparator<ItemType>() {
						public int compare(
								final ItemType a,
								final ItemType b) {
							final int ret = m_item2freq
									.get(a)
									.compareTo(m_item2freq
											.get(b));
							if (ret == 0) {
								return (a.equals(b) ? 0
										: System.identityHashCode(a)
												- System.identityHashCode(b));
							}
							return ret;
						}
					})
					: new TreeSet<ItemType>(
							new Comparator<ItemType>() {
								public int compare(
										final ItemType a,
										final ItemType b) {
									final int ret = m_item2freq
											.get(b)
											.compareTo(m_item2freq
													.get(a));
									if (ret == 0) {
										return (a.equals(b) ? 0
												: System.identityHashCode(b)
														- System.identityHashCode(a));
									}
									return ret;
								}
							});
			;
		}
		
		return sorted_items;
	}

	/**
	 * @author xujinghu
	 * @date 2012-12-3下午9:42:42
	 * @param sequence
	 * @return 序列的频繁度
	 */
	public Frequency getFrequency(Sequence<ItemType> sequence) {
		// TODO:还没实现此功能
		return new IntFrequency(1);
	}
}
